package ro.pub.cs.stadium.document.logic;
import java.util.Map;

import ro.pub.cs.stadium.document.AbstractDocument;
import ro.pub.cs.stadium.document.IState;
import ro.pub.cs.stadium.document.ITransition;

public class Document extends AbstractDocument {
	
	private final String docType = "logic-fsm";

	private VariableList vars;
	
	public Document() {
		super();
		vars = new VariableList();
	}
	
	public void addState(State S) {
		getStates().put(S.getName(), S);
	}

	public void addTransition(Transition T) {
		getTransitions().put(T.getName(), T);
	}
	
	public void removeState(State S) {
		Map<String,ITransition> ins = S.getInTransitions();
		Map<String,ITransition> outs = S.getOutTransitions();
		for (ITransition  T: ins.values()) removeTransition((Transition)T);
		for (ITransition  T: outs.values()) removeTransition((Transition)T);
		getStates().remove(S.getName());
		
	}
	
	public void removeTransition(Transition T) {
		getTransitions().remove(T.getName());
	}

	public VariableList getVariableList() {
		if (!getStates().isEmpty())
			for (IState s : getStates().values()) {
				vars.merge(((State)s).getVariables());
			}
		if (!getTransitions().isEmpty())	
			for (ITransition t : getTransitions().values()) {
				if (((Transition)t).isDefined())
				vars.merge(((Transition)t).getCondition().getVariables());
			}
		return vars;
	}
	
	public void removeVariable(Variable V) {
		vars.remove(V);
	}

	/* (non-Javadoc)
	 * @see ro.pub.cs.stadium.document.IDocument#getDocumentType()
	 */
	public String getDocumentType() {
		return docType;
	}

	public IState createNewState(String name) {
		State newState = new State(name, new VariableList());
		
		return (IState)newState;
	}

	public ITransition createNewTransition(String name) {
		Transition newTrans = new Transition(name);
		
		return (ITransition)newTrans;
	}
	
	
}
